<?php
final class connectorFactory {
	private static $CONEXAO;
	private static function conectar() {
		if (self::$CONEXAO == NULL) {
			self::$CONEXAO = mysql_connect ( HOST, USER, PASS ) or die ( "Erro ao conectar ao servidor &raquo; " . mysql_error () );
			self::$CONEXAO = mysql_select_db ( BASE ) or die ( "Erro ao selecionar o Banco de Dados &raquo; " . mysql_error () );
		}
		return self::$CONEXAO;
	}
	private static function fecharConexao() {
		self::$CONEXAO = NULL;
	}
	public static function query($consulta, $unico = false, $print = false) {
		if ($consulta) {
			connectorFactory::conectar ();
			
			$rs = mysql_query ( $consulta );
			$contar = mysql_numrows ( $rs );
			
			while ( $row = mysql_fetch_array ( $rs ) ) {
				$retorno [] = $row;
			}
			if ($print)
				print_r ( $retorno );
			connectorFactory::fecharConexao ();
		}
		if ($unico == false)
			return $retorno;
		else
			return $retorno [0];
	}
	public static function num_fields($consulta) {
		$result = mysql_query ( $consulta );
		return mysql_numfields ( $result );
	}
	public static function fields($consulta) {
		if ($consulta) {
			connectorFactory::conectar ();
			
			$query = mysql_query ( $consulta );
			$numfields = mysql_num_fields ( $query );
			
			$fielddata = mysql_fetch_array ( $query );
			
			for($i = 0; $i < $numfields; $i += 1) {
				$field = mysql_fetch_field ( $query, $i );
				$retorno [$i] = $field->name;
			}
			connectorFactory::fecharConexao ();
		}
		return $retorno;
	}
}

?>